package com.wngz.yudada.scoring;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wngz.yudada.model.dto.question.QuestionContentDto;
import com.wngz.yudada.model.entity.App;
import com.wngz.yudada.model.entity.Question;
import com.wngz.yudada.model.entity.ScoringResult;
import com.wngz.yudada.model.entity.UserAnswer;
import com.wngz.yudada.model.vo.QuestionVO;
import com.wngz.yudada.service.QuestionService;
import com.wngz.yudada.service.ScoringResultService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 自定义测评类应用评分策略
 * */
@ScoringStrategyConfig(appType = 1, scoringStrategy = 0)
public class CustomTestScoringStrategy implements ScoringStrategy{
    @Resource
    private QuestionService questionService;

    @Resource
    private ScoringResultService scoringResultService;


    @Override
    public UserAnswer doScore(List<String> choices, App app) throws Exception {
        Long appId = app.getId();

        //根据 id 查询到题目和题目结果信息
        Question question = questionService.getOne(
                Wrappers.lambdaQuery(Question.class)
                        .eq(Question::getAppId, appId));

        List<ScoringResult> scoringResultList = scoringResultService.list(
                Wrappers.lambdaQuery(ScoringResult.class)
                        .eq(ScoringResult::getAppId, appId));

        //统计用户每个选择对应的属性个数，如I=10个，E-5个
        //创建存储得分的Map
        Map<String, Integer> optionCount = new HashMap<>();
        QuestionVO questionVO = QuestionVO.objToVo(question);
        List<QuestionContentDto> questionContent = questionVO.getQuestionContent();

        //遍历题目列表
        for (QuestionContentDto questionContentDtoDto : questionContent) {
            //遍历答案列表
            for (String answer : choices) {
                //遍历题目中的选项
                for (QuestionContentDto.Option option : questionContentDtoDto.getOptions()) {
                    //如果答案和选项key匹配
                    if (option.getKey().equals(answer)) {
                        //获取选项的result
                        String result = option.getResult();
                        //如果result不在optionCount中，初始化0
                        if (!optionCount.containsKey(result)) {
                            optionCount.put(result, 0);
                        }
                        //在optionCount中增加计数
                        optionCount.put(result, optionCount.get(result) + 1);
                    }
                }
            }
        }
        //遍历每种评分结果，计算哪个结果的得分更高。
        //初始化最高分数和最高分数的对应的评分结果
        int maxScore = 0;
        ScoringResult maxScoreResult = scoringResultList.get(0);
        //遍历评分结果列表
        for (ScoringResult scoringResult : scoringResultList) {
            List<String> resultProp = JSONUtil.toList(scoringResult.getResultProp(), String.class);
            //计算当前评分结果分数[I，E] => [10,5] = 15
            int score = resultProp.stream()
                    .mapToInt(prop -> optionCount.getOrDefault(prop, 0))
                    .sum();

            //如果当前评分结果分数大于最高分数，更新最高分数和最高分数的对应的评分结果
            if (score > maxScore) {
                maxScore = score;
                maxScoreResult = scoringResult;
            }
        }
        //构造返回值，填充答案对象的属性。

        UserAnswer userAnswer = new UserAnswer();
        userAnswer.setAppId(appId);
        userAnswer.setAppType(app.getAppType());
        userAnswer.setScoringStrategy(app.getScoringStrategy());
        userAnswer.setChoices(JSONUtil.toJsonStr(choices));
        userAnswer.setResultId(maxScoreResult.getId());
        userAnswer.setResultName(maxScoreResult.getResultName());
        userAnswer.setResultDesc(maxScoreResult.getResultDesc());
        userAnswer.setResultPicture(maxScoreResult.getResultPicture());

        return userAnswer;
    }
}
